package kr.ragne.framework;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class SpriteAnimation extends GraphicObject {
	private Rect rect;		// 그려줄 사각 영역
	private int mFps;		// 초당 프레임
	private int iFrames;	// 프레임 개수
	
	private int currentFrame;	// 최근 프레임
	private int spriteWidth;
	private int spriteHeight;
	
	private long frameTimer;	// 이전 시간 정보를 담을 변수

	public SpriteAnimation(Bitmap bitmap) {
		super(bitmap);
		rect = new Rect(0, 0, 0, 0);
		currentFrame = 0;
		frameTimer = 0;
	}
	
	public void InitSpriteData(int width, int height, int fps, int frame){
		// 기본 정보 설정
		spriteWidth = width;
		spriteHeight = height;
		rect.top = 0;
		rect.bottom = spriteHeight;
		rect.left = 0;
		rect.right = spriteWidth;
		mFps = 1000 / fps;
		iFrames = frame;
	}
	
	@Override
	public void Draw(Canvas canvas) {
		Rect dest = new Rect(mX, mY, mX + spriteWidth, mY + spriteHeight);
		canvas.drawBitmap(mBitmap, rect, dest, null);
	}
	
	public void Update(long gameTime) {
		if(gameTime > frameTimer + mFps){
			frameTimer = gameTime;
			currentFrame += 1;
			if(currentFrame >= iFrames){
				currentFrame = 0;
			}
		}
		rect.left = currentFrame * spriteWidth;
		rect.right = rect.left + spriteWidth;
	}
	
}
